$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'table/list',
        datatype: "json",
        colModel: [
            {label: '餐桌id', name: 'id', width: 30},
            {label: '餐桌编号', name: 'tableNum', index: "table_num", width: 20},
            {label: '餐桌名称', name: 'tableName',index:"table_name", width: 20},
            {
                label: '餐桌状态', name: 'tableStatus',index:"table_status", width: 20,
                formatter: function (value, options, row) {
                    if (value == 2) {
                        return '<span class="label label-info">点餐中</span>';
                    } else if (value == 1) {
                        return '<span class="label label-success">空闲</span>';
                    } else if (value == 3) {
                        return '<span class="label label-danger">就餐中</span>';
                    } else {
                        return "";
                    }
                }
            },
        ],
        viewrecords: true,
        height: 720,
        rowNum: 19,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    $("#jqGridType").jqGrid({
        url: baseURL + 'table/tableTypeListPage',
        datatype: "json",
        colModel: [
            {label: '类型id', name: 'id', width: 30},
            {label: '类型名称', name: 'name', index: "name", width: 30},
            {label: '最大承载人数', name: 'maxNum',index:"max_num", width: 30},
            {label: '最小承载人数', name: 'minNum',index:"min_num", width: 30},
        ],
        viewrecords: true,
        height: 720,
        rowNum: 19,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPagerType",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGridType").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

});

var vm = new Vue({
    el: '#dcapp',
    data: {
        selected: '',
        q: {
            name: null,
        },
        q2: {
            name: null,
        },
        showList: 1,
        title: null,
        table: {},
        tableType: {},
        options: [],
        formJson: null,
        showTypeList: 1,
    },
    methods: {
        query: function () {
            vm.reload();
        },
        queryType: function () {
            vm.reloadType();
        },
        update: function () {
            vm.getTableType();
            var tableId = getSelectedRow();//选择一条
            if (tableId == null) {
                return;
            }
            vm.getTable(tableId);
            vm.showList = 2;
            vm.title = "编辑餐桌";
        },
        add: function () {
            vm.getTableType();
            vm.table = {};
            vm.showList = 2;
            vm.title = "增加餐桌";
        },
        saveOrUpdate: function () {
            var url = vm.table.id == null?"table/saveTable":"table/updateTable";
            vm.formJson = JSON.stringify(vm.table);
                $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: vm.formJson,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },
        del: function () {
            var tableIds = getSelectedRows();
            if (tableIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "table/delete",
                    contentType: "application/json",
                    data: JSON.stringify(tableIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getTable: function (tableId) {
            $.ajax({
                type: "GET",
                url: baseURL + "table/info/" + tableId,
                success: function(r){
                    vm.table = r.table;
                }
            });

        },
        getTableType: function () {
            $.get(baseURL + "table/tableTypeList", function (r) {
                vm.options = r.tableTypes;
            });
        },
        reload: function (event) {
            vm.showList = 1;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },
        reloadType: function (event) {
            vm.showTypeList = 1;
            var page = $("#jqGridType").jqGrid('getGridParam', 'page');
            $("#jqGridType").jqGrid('setGridParam', {
                postData: {'key': vm.q2.key},
                page: page
            }).trigger("reloadGrid");
        },
        getTableTypeInfo: function (tableTypeId) {
            $.ajax({
                type: "GET",
                url: baseURL + "table/tableType/" + tableTypeId,
                success: function(r){
                    vm.tableType = r.tableType;
                }
            });

        },
        updateType: function () {
            var tableTypeId = getSelectedRowType();//选择一条

            if (tableTypeId == null) {
                return;
            }
            vm.getTableTypeInfo(tableTypeId);
            vm.showTypeList = 2;
            vm.title = "编辑餐桌类型";
        },
        addType: function () {
            vm.tableType = {};
            vm.showTypeList = 2;
            vm.title = "增加餐桌类型";
        },
        delType: function () {
            var tableIds = getSelectedRowsType();
            if (tableIds == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "table/deleteType",
                    contentType: "application/json",
                    data: JSON.stringify(tableIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reloadType();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdateType: function () {
            var url = vm.tableType.id == null?"table/saveTableType":"table/updateType";
            vm.formJson = JSON.stringify(vm.tableType);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: vm.formJson,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reloadType();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },

    }
});







